package com.ohy.tencent.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("rr")
public class RenRenVideoController {

    @GetMapping(value = "getClass")
    public String getAllClass() {
        String key = "video:rr:Class";
        String url = "http://pc.allappapi.com/index.php?g=api/pv3&m=index&client=5&accesskey=519f9cab85c8059d17544947k361a827&a=resource_category";
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        return execute.body();
    }

    @GetMapping(value = "getAllVideo")
    public String getAllVideo(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                              @RequestParam(value = "channel", required = false, defaultValue = "movie") String channel,
                              @RequestParam(value = "area", required = false, defaultValue = "") String area,
                              @RequestParam(value = "category", required = false, defaultValue = "") String category,
                              @RequestParam(value = "year", required = false, defaultValue = "") String year,
                              @RequestParam(value = "lang", required = false, defaultValue = "") String lang,
                              @RequestParam(value = "order", required = false, defaultValue = "itemupdate") String order
    ) {
        //http://pc.allappapi.com/index.php?g=api/pv3&m=index&client=5&accesskey=519f9cab85c8059d17544947k361a827&a=resource_storage&uid=&token=&channel=movie&area=%E7%BE%8E%E5%9B%BD&category=%E5%8A%A8%E4%BD%9C&year=2001&subject=&tv=&lang=%E6%97%A5%E8%AF%AD&order=dateline&page=1&limit=30
        String url = "http://pc.allappapi.com/index.php?g=api/pv3&m=index&client=5&accesskey=519f9cab85c8059d17544947k361a827&a=resource_storage&uid=&token=&limit=30&" +
                "channel=" + channel +
                "&area=" + area +
                "&category=" + category +
                "&year=" + year +
                "&subject=&tv=" +
                "&lang=" + lang +
                "&order=" + order +
                "&page=" + page;
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        return execute.body();
    }

    @GetMapping(value = "getSearchRes")
    public String getSearch(@RequestParam(value = "word") String word) {
        String url = "http://pc.allappapi.com/index.php?g=api/pv3&m=index&client=5&accesskey=519f9cab85c8059d17544947k361a827&a=search&uid=&token=&page=1&limit=30&k=" + word;
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        JSONObject jsonObject = JSONObject.parseObject(execute.body());
        List<JSONObject> list = new ArrayList<>();
        for (int i = 0; i < jsonObject.getJSONArray("data").size(); i++) {
            JSONObject object = new JSONObject();
            object.put("score", jsonObject.getJSONArray("data").getJSONObject(i).getString("score"));
            object.put("title", jsonObject.getJSONArray("data").getJSONObject(i).getString("cnname"));
            object.put("pic", jsonObject.getJSONArray("data").getJSONObject(i).getString("poster"));
            object.put("url", jsonObject.getJSONArray("data").getJSONObject(i).getString("id"));
            object.put("type", jsonObject.getJSONArray("data").getJSONObject(i).getString("channel"));
            list.add(object);
        }
        return list.toString();
    }

    @GetMapping(value = "getVideoInfo")
    public String getVideoInfo(@RequestParam(value = "id") String id) {
        String url = "http://pc.allappapi.com/index.php?g=api/pv3&m=index&client=5&accesskey=519f9cab85c8059d17544947k361a827&a=resource&id=" + id + "&uid=undefined&token=undefined";
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        String res = execute.body().replaceAll("HR-HDTV", "MP4");
        return res;
    }

}
